package com.cr.business.service.impl;

import com.cr.business.domain.TbProducerInfo;
import com.cr.business.domain.dto.ProducerReqDto;
import com.cr.business.domain.dto.app.ProducerEditParam;
import com.cr.business.domain.dto.app.ProducerListParam;
import com.cr.business.domain.vo.AppProducerInfoData;
import com.cr.business.domain.vo.AppProducerListData;
import com.cr.business.domain.vo.AppProducerOptionData;
import com.cr.business.domain.vo.ProducerDataVo;
import com.cr.business.mapper.TbProducerInfoMapper;
import com.cr.business.service.ITbProducerInfoService;
import com.cr.common.utils.DateUtils;
import com.cr.common.utils.SecurityUtils;
import com.cr.common.utils.uuid.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产人信息Service业务层处理
 * 
 * @author tang
 * @date 2025-08-19
 */
@Service
public class TbProducerInfoServiceImpl implements ITbProducerInfoService 
{
    @Resource
    private TbProducerInfoMapper tbProducerInfoMapper;

    @Override
    public List<ProducerDataVo> selectTbProducerInfoList(ProducerReqDto dto)
    {
        return tbProducerInfoMapper.selectTbProducerInfoList(dto);
    }

    @Override
    public int updateProducerStatus(TbProducerInfo tbProducerInfo) {
        return tbProducerInfoMapper.updateTbProducerInfo(tbProducerInfo);
    }

    /**
     * 查询生产人信息
     * 
     * @param producerId 生产人信息主键
     * @return 生产人信息
     */
    @Override
    public AppProducerInfoData selectTbProducerInfoByProducerId(String producerId)
    {
        return tbProducerInfoMapper.selectAppProducerInfoByProducerId(producerId);
    }

    /**
     * 批量删除生产人信息
     * 
     * @param producerIds 需要删除的生产人信息主键
     * @return 结果
     */
    @Override
    public int deleteTbProducerInfoByProducerIds(String[] producerIds)
    {
        return tbProducerInfoMapper.deleteTbProducerInfoByProducerIds(producerIds);
    }

    /**
     * 删除生产人信息信息
     * 
     * @param producerId 生产人信息主键
     */
    @Override
    public int deleteTbProducerInfoByProducerId(String producerId)
    {
        return tbProducerInfoMapper.deleteTbProducerInfoByProducerId(producerId);
    }

    @Override
    public Map<String, Object> getAppList(ProducerListParam param) {
        // 当前用户
        String appUserId = SecurityUtils.getAppUserId();

        // 分页查询
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<AppProducerListData> appProducerListData = tbProducerInfoMapper.selectAppProducerListData(appUserId);
        PageInfo<AppProducerListData> pageInfo = new PageInfo<>(appProducerListData);
        PageHelper.clearPage();

        // 封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("data", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    /**
     * 新增生产人信息
     *
     * @param param 生产人信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTbProducerInfo(ProducerEditParam param)
    {
        TbProducerInfo byPhone = tbProducerInfoMapper.selectByPhone(param.getPhone());
        if (byPhone != null) {
            throw new RuntimeException("生产人手机号已存在！");
        }

        String isDefault = param.getIsDefault();
        if("1".equals(isDefault)) {
            // 如果当前生产人是默认生产人，则取消其他默认生产人
            tbProducerInfoMapper.cancelOtherDefault(SecurityUtils.getAppUserId());
        }

        TbProducerInfo tbProducerInfo = copyInfo(param);
        tbProducerInfo.setProducerId(IdUtils.simpleUUID());
        tbProducerInfo.setUserId(SecurityUtils.getAppUserId());
        tbProducerInfo.setCreateTime(DateUtils.getNowDate());
        return tbProducerInfoMapper.insertTbProducerInfo(tbProducerInfo);
    }

    /**
     * 修改生产人信息
     *
     * @param params 生产人信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTbProducerInfo(ProducerEditParam params)
    {

        TbProducerInfo byPhone = tbProducerInfoMapper.selectByPhone(params.getPhone());
        if(byPhone != null && !byPhone.getProducerId().equals(params.getId())) {
            throw new RuntimeException("生产人手机号已存在！");
        }

        String isDefault = params.getIsDefault();
        if("1".equals(isDefault)) {
            // 如果当前生产人是默认生产人，则取消其他默认生产人
            tbProducerInfoMapper.cancelOtherDefault(SecurityUtils.getAppUserId());
        }

        TbProducerInfo tbProducerInfo = copyInfo(params);
        tbProducerInfo.setProducerId(params.getId());
        return tbProducerInfoMapper.updateTbProducerInfo(tbProducerInfo);
    }

    private TbProducerInfo copyInfo(ProducerEditParam param) {
        TbProducerInfo tbProducerInfo = new TbProducerInfo();
        tbProducerInfo.setProducerName(param.getName());
        tbProducerInfo.setProducerPhone(param.getPhone());
        tbProducerInfo.setSignImg(param.getSignImg());
        tbProducerInfo.setIsDefault(param.getIsDefault());
        tbProducerInfo.setUpdateTime(DateUtils.getNowDate());
        return tbProducerInfo;
    }

    @Override
    public List<AppProducerOptionData> getAppOptionData() {
        // 当前用户
        String appUserId = SecurityUtils.getAppUserId();
        return tbProducerInfoMapper.selectAppOptionListData(appUserId);
    }

}
